﻿using System;
using Microsoft.AspNetCore.Mvc;
using xstore.service;

namespace xstore.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BucketController : ControllerBase
    {

        private readonly S3ClientToCtyun s3Client = S3ClientToCtyun.GetInstance();

        // api/bucket
        [HttpGet]
        public string GetAllBucket()
        {
            try
            {
                return String.Join(", ", s3Client.ListAllBuckets());
            }
            catch (Exception e)
            {
                return "error! " + e.Message;
            }
        }

        // api/bucket/{bucket}
        [HttpGet("{bucket}")]
        public string ListObjectInBucket(string bucket)
        {

            try
            {
                return String.Join(", ", s3Client.ListAllObjectKeysInBucket(bucket));
            }
            catch (Exception e)
            {
                return "error! " + e.Message;
            }
        }

        // api/bucket/create
        // Body: bucket={bucket}
        [HttpPost("create")]
        public string Create([FromForm] string bucket)
        {
            try
            {
                return s3Client.CreateBucket(bucket);
            }
            catch (Exception e)
            {
                return "error! " + e.Message;
            }
        }

        [HttpDelete("{bucket}")]
        public string Delete(string bucket)
        {
            try
            {
                return s3Client.DeleteBucket(bucket);
            }
            catch (Exception e)
            {
                return "error! " + e.Message;
            }
        }
    }

}
